//
//  ECMemberSideslipView.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/11/4.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECMemberSideslipView.h"
#import "ECMemberFiltrateTableViewCell.h"

@interface ECMemberSideslipView()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) UIButton * makesureBT;

@property (nonatomic,strong) UIButton * resetBT;

@property (nonatomic,strong) NSArray * dataArr;

@end

@implementation ECMemberSideslipView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
    }
    return self;
}

- (void)setupUI {
    WS(weakSelf, self);
    UIView * view = [UIView new];
    
    WS(weakV, view);
    self.resetBT = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.resetBT setTitle:@"重置" forState:UIControlStateNormal];
    [self.makesureBT setBackgroundImage:[UIImage imageWithColor:ECCOLOR(0xFFFFFF)] forState:UIControlStateNormal];
    self.resetBT.titleLabel.font = ECFONT(18);
    [self.resetBT setTitleColor:ECCOLOR(0x41D95A) forState:UIControlStateNormal];
    [self.resetBT addTarget:self action:@selector(doReset:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.resetBT];
    [self.resetBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(weakV);
        make.width.mas_equalTo(162 * PX_RELATIVE375);
    }];
    
    self.makesureBT = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.makesureBT setTitle:@"确定" forState:UIControlStateNormal];
    [self.makesureBT setBackgroundImage:[UIImage imageWithColor:ECCOLOR(0x41D95A)] forState:UIControlStateNormal];
    self.makesureBT.titleLabel.font = ECFONT(18);
    [self.makesureBT setTitleColor:ECCOLOR(0xFFFFFF) forState:UIControlStateNormal];
    [view addSubview:self.makesureBT];
    [self.makesureBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(weakV);
        make.width.mas_equalTo(162 * PX_RELATIVE375);
    }];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(60);
        if (@available(iOS 11.0,*)) {
            make.bottom.mas_equalTo(weakSelf.mas_safeAreaLayoutGuideBottom);
        }else {
            make.bottom.mas_equalTo(weakSelf);
        }
    }];
    
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(weakSelf);
        make.bottom.mas_equalTo(weakV.mas_top);
    }];
}

- (void)doReset:(UIButton*)sender {
    for (NSMutableDictionary * muDic in self.dataArr) {
        muDic[@"value"] = @"";
    }
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ECMemberFiltrateTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSDictionary * dic = self.dataArr[indexPath.row];
    if (dic[@"value"] && ((NSString*)dic[@"value"]).length > 0) {
        cell.detailLB.textColor = ECCOLOR(0x333333);
        cell.detailLB.text = (NSString*)dic[@"value"];
    }else {
        cell.detailLB.textColor = ECCOLOR(0xcccccc);
        cell.detailLB.text = (NSString*)dic[@"placeHolder"];
    }
    cell.titleLB.text = dic[@"title"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.0001)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.0001)];
        [_tableView registerClass:[ECMemberFiltrateTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = ECCOLOR(0xFFFFFF);
        _tableView.bounces = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (NSArray *)dataArr {
    if (!_dataArr) {
        _dataArr = @[@{@"title":@"订单状态",@"placeHolder":@"请选择",@"value":@""}.mutableCopy,
                     @{@"title":@"客户来源",@"placeHolder":@"请选择",@"value":@""}.mutableCopy,
                     @{@"title":@"客户类型",@"placeHolder":@"请选择",@"value":@""}.mutableCopy];
    }
    return _dataArr;
}

@end
